#define _CRT_SECURE_NO_WARNINGS 1
#include "Seqlist.h"


void SLCheckCapacity(Seqlist* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		int newcapacity = ((ps->capacity == 0) ? 4 : (ps->capacity) * 2);
		SLdatetype* tmp = (SLdatetype*)realloc(ps->s, newcapacity * sizeof(SLdatetype));
		if (tmp != NULL)
		{
			ps->s = tmp;
			ps->capacity = newcapacity;
		}
		else
		{
			perror("realloc ps->Size");
			exit(-1);
		}
	}
}


void SLinit(Seqlist* ps)
{
	assert(ps);
	ps->s = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

void SeqListDestroy(Seqlist* ps)
{
	assert(ps);
	free(ps->s);
	ps->s = NULL;
	ps->size = ps->capacity = 0;
}

void SLpushback(Seqlist* ps, SLdatetype x)
{
	assert(ps);
	//if (ps->size == ps->capacity)
	//{
	//	int newcapacity = ((ps->capacity == 0 )? 4 : (ps->capacity) * 2);
	//	SLdatetype* tmp = (SLdatetype*)realloc(ps->s, newcapacity * sizeof(SLdatetype));
	//	if (tmp != NULL)
	//	{
	//		ps->s = tmp;
	//		ps->capacity = newcapacity;
	//	}
	//	else
	//	{
	//		assert(tmp);
	//	}
	//}
	SLCheckCapacity(ps);
	ps->s[ps->size] = x;
	ps->size++;
}

void SLprint(Seqlist* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->s[i]);
	}
}

void SLpopback(Seqlist* ps)
{
	assert(ps);
	assert(ps->size > 0);
	ps->size--;
}

void SLpushfront(Seqlist* ps, SLdatetype x)
{
	assert(ps);
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->s[end+1] = ps->s[end];
		end--;
	}
	ps->s[0] = x;
	ps->size++;
}


void SLpopfront(Seqlist* ps)
{
	assert(ps);
	assert(ps->size > 0);
	int end = 0;
	while (end < ps->size)
	{
		ps->s[end] = ps->s[end + 1];
		end++;
	}
	ps->size--;
}


void SLinsert(Seqlist* ps, int pos, SLdatetype x)
{
	assert(ps);
	assert(pos <= ps->size);
	assert(pos >= 0);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->s[end + 1] = ps->s[end];
		end--;
	}
	ps->s[pos] = x;
	ps->size++;
}

void SLerase(Seqlist* ps, int pos)
{
	assert(ps);
	assert(pos <= ps->size);
	assert(pos >= 0);
	int end = pos;
	while (end < ps->size)
	{
		ps->s[end] = ps->s[end + 1];
		end++;
	}
	ps->size--;
}


int SLfine(Seqlist* ps, SLdatetype x, int begin)
{
	assert(ps);
	int i = 0;
	for (i = begin; i < ps->size; i++)
	{
		if (ps->s[i] == x)
		{
			return i;
		}
	}
	return -1;
}